package com.gopay.common.oldmermove.manager.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gopay.common.constants.cust.UserInfoConstants;
import com.gopay.common.domain.UserInfo;
import com.gopay.common.domain.oldmermove.OldMovedMer;
import com.gopay.common.oldmermove.dao.OldMovedMerDAO;
import com.gopay.common.oldmermove.manager.OldMovedMerQueryManager;

/**
 * 旧平台迁移商户Manager实现
 * 
 * @OldMovedMerQueryManagerImpl.java
 * @author zhangjunyu
 * @2014-9-24 下午2:17:48 www.gopay.com.cn Inc.All rights reserved.
 */
@Service("oldMovedMerQueryManager")
public class OldMovedMerQueryManagerImpl implements OldMovedMerQueryManager {

	@Autowired
	private OldMovedMerDAO oldMovedMerDAO;
	
	/**
	 * 根据新平台商户号查询
	 */
	@Override
	public OldMovedMer getByNewMerId(String newMerId) {
		return oldMovedMerDAO.getByNewMerId(newMerId);
	}

	/**
	 * 是否为旧平台迁移用户，管理员用户首次登录<br>
	 * 条件：密码字段为空，usertype='20'(企业管理员),OldMovedMer表存在记录;
	 */
	@Override
	public boolean isAdminFirstLogin(UserInfo userInfo) {
		/* !(密码字段为空&&usertype='20') */
		if (!(StringUtils.isEmpty(userInfo.getPassword()) && UserInfoConstants.USER_TYPE_CORP_ADMIN.equals(userInfo
				.getUserType()))) {
			return false;
		}
		OldMovedMer oldMer = oldMovedMerDAO.getByNewMerId(userInfo.getCustId());
		return oldMer != null;
	}

	/**
	 * 旧平台迁移商户 企业下级用户首次登录<br>
	 * 条件：密码问题为空，密码答案为空，usertype='21'||'22',OldMovedMer表存在记录;
	 */
	@Override
	public boolean isSubUserFirstLogin(UserInfo userInfo) {
		/* !(密码问题为空&&密码答案为空&&usertype='21'||'22') */
		if (!(StringUtils.isEmpty(userInfo.getPasswordQues()) && StringUtils.isEmpty(userInfo.getPasswordQuesAns()) && (UserInfoConstants.USER_TYPE_CORP_FINANCE
				.equals(userInfo.getUserType()) || UserInfoConstants.USER_TYPE_CORP_TRANS
				.equals(userInfo.getUserType())))) {
			return false;
		}
		OldMovedMer oldMer = oldMovedMerDAO.getByNewMerId(userInfo.getCustId());
		return oldMer != null;
	}

	/**
	 * 更新对象
	 */
	@Override
	public void update(OldMovedMer oldMer) {
		oldMovedMerDAO.update(oldMer);		
	}
}
